<template>
  <div class="app-container">
    <div class="top-bar-container">
      <el-row>
        <el-col :span="16">
          <div class="search-container">
            <search-form @search-submit="getDictConfigList">
              <search-input v-model="listQuery.dictName" placeholder="字典名称" />
              <search-input v-model="listQuery.dictKey" placeholder="字典Key" />
            </search-form>
          </div>
        </el-col>
        <el-col :span="8">
          <el-row type="flex" justify="end">
            <el-button type="primary" size="small" icon="el-icon-plus" @click="handleCreateDictConfig">新增字典
            </el-button>
          </el-row>
        </el-col>
      </el-row>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit stripe highlight-current-row>
      <el-table-column align="center" label="ID">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="字典名称">
        <template slot-scope="{row}">
          <span>{{ row.dictName }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="字典Key">
        <template slot-scope="{row}">
          <span>{{ row.dictKey }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="字典描述">
        <template slot-scope="{row}">
          <span>{{ row.description }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="创建时间">
        <template slot-scope="{row}">
          <span>{{ row.createdTime }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="更新时间">
        <template slot-scope="{row}">
          <span>{{ row.updatedTime }}</span>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        fixed="right"
        label="操作"
        width="280"
      >
        <template slot-scope="scope">
          <el-button-group>
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-folder-opened"
              title="查看字典条目"
              @click="handleDisplayDictConfigItemList(scope.row.id,scope.row.dictName)"
            >字典条目
            </el-button>
            <el-button
              type="warning"
              size="mini"
              icon="el-icon-edit"
              title="编辑"
              @click="handleEditDictConfigRow(scope.$index)"
            >编辑
            </el-button>
            <el-button
              type="danger"
              size="mini"
              icon="el-icon-delete"
              title="删除"
              @click="handleDeleteDictConfigRow(scope.$index)"
            >删除
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getDictConfigList"
    />

    <el-dialog
      :title="editForm.id ? '编辑字典' : '新增字典'"
      :visible.sync="dialogEditVisible"
      width="500px"
      @close="handleEditFormDialogClose"
    >
      <el-form
        ref="editForm"
        :model="editForm"
        :rules="editRules"
        class="edit-form"
        autocomplete="on"
        label-position="right"
        label-width="80px"
      >
        <el-form-item prop="dictName" label="字典名称">
          <el-input v-model="editForm.dictName" />
        </el-form-item>
        <el-form-item prop="dictKey" label="字典Key">
          <el-input v-model="editForm.dictKey" />
        </el-form-item>
        <el-form-item prop="description" label="字典描述">
          <el-input v-model="editForm.description" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSaveDictConfig">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination/index'
import SearchForm from '@/components/SearchForm/index'
import SearchInput from '@/components/SearchForm/SearchInput/index'
import {
  dictConfigCheckAvailable,
  dictConfigCreate,
  dictConfigDelete,
  dictConfigList,
  dictConfigUpdate
} from '@/api/rest/dictconfig'
import { yesNoStatusList } from '@/utils/dict'
import { elRequiredRule } from '@/utils/el-validator'

const initForm = {
  id: null,
  dictName: null,
  dictKey: null,
  description: null
}

export default {
  name: 'DictConfigList',
  components: { SearchInput, Pagination, SearchForm },
  data() {
    const validateDictKeyAvailable = (rule, value, callback) => {
      return dictConfigCheckAvailable(value, this.editForm.id).then(resp => {
        if (!resp.data) {
          callback(new Error('字典Key已存在'))
        } else {
          callback()
        }
      })
    }

    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        id: null,
        dictName: null,
        dictKey: null,
        page: 1,
        limit: 10
      },
      editForm: { ...initForm },
      editRules: {
        dictName: elRequiredRule,
        dictKey: [elRequiredRule, { trigger: 'blur', validator: validateDictKeyAvailable }]
      },
      ancestors: null,
      yesNoStatusList: yesNoStatusList,
      dialogEditVisible: false
    }
  },
  watch: {
    $route(to, from) {
      this.getDictConfigList()
    }
  },
  created() {
    this.getDictConfigList()
  },
  methods: {
    getDictConfigList() {
      this.listLoading = true
      dictConfigList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleEditDictConfigRow(index) {
      this.editForm = { ...this.list[index] }
      this.dialogEditVisible = true
    },
    handleDeleteDictConfigRow(index) {
      const data = this.list[index]
      this.$confirm('此操作将删除字典, 是否继续?', '提示', {
        type: 'warning'
      }).then(() => {
        dictConfigDelete(data.id).then(() => {
          this.$message({
            type: 'success',
            message: '删除字典成功'
          })
          this.getDictConfigList()
        }).catch(() => {
        })
      })
    },
    handleCreateDictConfig() {
      this.editForm = { ...initForm }
      this.dialogEditVisible = true
    },
    handleSaveDictConfig() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          if (this.editForm.id) {
            dictConfigUpdate(this.editForm).then(response => {
              this.$message({
                message: '编辑字典成功',
                type: 'success'
              })
              this.getDictConfigList()
              this.dialogEditVisible = false
            })
          } else {
            dictConfigCreate(this.editForm).then(response => {
              this.$message({
                message: '新增字典成功',
                type: 'success'
              })
              this.getDictConfigList()
              this.dialogEditVisible = false
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleDisplayDictConfigItemList(dictConfigId, dictConfigName) {
      this.$router.push({
        path: '/platform/dictConfigItem/list',
        query: { dictConfigId: dictConfigId, dictConfigName: dictConfigName }
      })
    },
    handleEditFormDialogClose() {
      this.$refs.editForm.clearValidate()
    }
  }
}
</script>
<style scoped>
</style>

